<?php
$title_page = "Sửa đổi thông tin sản phẩm";
require_once('include/permiss.php');
require_once('include/header.php');
require_once('include/menu.php');
require_once('config.php');

if (isset($_POST['submit'])) {
    $error = array();

    //Kiểm tra giá trị giỗng hay không	
    $arry = array('product_label' => 'Mã sản phẩm', 'product_name' => 'Tên sản phẩm', 'product_price' => 'Giá sản phẩm', 'product_time' => 'Thời gian');
    foreach ($arry as $key => $value) {
        if (!isset($_POST[$key]) || empty($_POST[$key])) {
            $error[] = "<font color=red>" . $value . "</font> chưa có thông tin";
        }
    }
    $index_id = $_POST['index_id'];
    if (!count($error)) {
        $product_name = mysql_real_escape_string($_POST['product_name']);
        $product_label = mysql_real_escape_string($_POST['product_label']);

        $pdo = $conn->prepare('UPDATE datab_product SET product_label=?, product_name=?, product_price=?, product_time=?, product_detail=?, index_id=?, product_modifi=? WHERE product_id=?');
        $pdo->execute(array($product_label, $product_name, $_POST['product_price'], $_POST['product_time'], $_POST['product_detail'], $_POST['index_id'], date("Y-m-d H:i:s") ,$_POST['product_id']));

        if ($pdo->rowCount()) {
            $message = 'Cập nhật thành công!';
        } else {
            $error[] = 'Không thể cập nhật';
        }
    }
}//hết isset submit
?>
<div class="container">
    <div class="box">
        <div class="box-heading">Sản phẩm</div>
        <div class="box-content">
            <ul>
                <?php
                if (!empty($error)) {
                    foreach ($error as $k) {
                        echo "<li style='color:red'>" . $k . "</li>";
                    }
                }
                if (isset($message)) {
                    echo '<li style=\'color:blue\'>' . $message . '</li>';
                }
                ?>
            </ul>
            <form method="post" enctype="multipart/form-data" action=''>
                <table class="addlist">
                    <?php
                    $product_id = $_GET['product_id'];
                    $pdo = $conn->prepare('SELECT * FROM datab_product WHERE product_id=? LIMIT 1');
                    $pdo->execute(array($product_id));
                    if ($pdo->rowCount()) {
                        $data = $pdo->fetchAll();
                        foreach ($data as $row) {
                            ?>
                            <tr bgcolor="#EFEFEF">
                                <td class="modem">Mã ID:</td>
                                <td class="name"><?php echo $row['product_id'] ?><input type="hidden" name="product_id" size="3" value="<?php echo $row['product_id']; ?>" /></td>
                            </tr>
                            <tr>
                                <td class="modem">Mã sản phẩm*:</td>
                                <td class="name"><input type="text" name="product_label" size="30" value="<?php echo $row['product_label']; ?>" /></td>
                            </tr>
                            <tr>
                                <td class="modem">Tên sản phẩm*:</td>
                                <td class="name"><input type="text" name="product_name" size="40" value="<?php echo $row['product_name']; ?>" /></td>
                            </tr>
                            <tr>
                                <td class="modem">Giá*:</td>
                                <td class="name"><input type="text" name="product_price" size="7" value="<?php echo $row['product_price']; ?>" />VNĐ</td>
                            </tr>
                            <tr>
                                <td class="modem">Thời gian:</td>
                                <td class="name"><input type="text" name="product_time" size="3" value="<?php echo $row['product_time']; ?>"/>phút</td>
                            </tr>

                            <tr>
                                <td class="modem">Thể loại*:</td>
                                <td class="name">
                                    <select name="index_id">
                                        <option value="1" <?php if ($row['index_id'] == 1) echo 'selected=\'selected\''; ?>>Món ăn nhanh</option>
                                        <option value="2" <?php if ($row['index_id'] == 2) echo 'selected=\'selected\''; ?>>Món bánh</option>
                                        <option value="3" <?php if ($row['index_id'] == 3) echo 'selected=\'selected\''; ?>>Đồ uống</option>
                                    </select>
                                </td>
                            </tr>
                            <script type="text/javascript" src="/editor/ckeditor/ckeditor.js"></script>
                            <tr>
                                <td class="modem">Mô tả:</td>
                                <td class="name">
                                    <textarea class='ckeditor' name="product_detail">
                                        <?php echo $row['product_detail']; ?>
                                    </textarea>
                                </td>
                            </tr>
                            <tr>
                                <td></td>
                                <td><input type="submit" name='submit' value="Sửa" /></td>
                            </tr>
                            <?php
                        }
                    }
                    ?>
                </table>
            </form>
        </div>
    </div>    
</div>
<?php
require_once('include/footer.php');
?>
 
